package cc.jq1024.domain.strategy.service.rule.tree.factory.engine.impl;

import cc.jq1024.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cc.jq1024.domain.strategy.model.valobj.RuleTreeNodeLineVO;
import cc.jq1024.domain.strategy.model.valobj.RuleTreeNodeVO;
import cc.jq1024.domain.strategy.model.valobj.RuleTreeVO;
import cc.jq1024.domain.strategy.service.rule.tree.ILogicTreeNode;
import cc.jq1024.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import cc.jq1024.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 规则树工厂 - 执行引擎 - 规则树组合接口实现
 * @Date: 2024-04-16-14:09
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {

    private final Map<String, ILogicTreeNode> logicTreeNodeGroupMap;

    private final RuleTreeVO ruleTreeVO;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> logicTreeNodeGroupMap, RuleTreeVO ruleTreeVO) {
        this.logicTreeNodeGroupMap = logicTreeNodeGroupMap;
        this.ruleTreeVO = ruleTreeVO;
    }

    /**
     * 规则树过滤方法 -> 库存？枷锁？等
     * @param userId 用户id
     * @param strategyId 策略Id
     * @param awardId 奖品id
     * @return 奖品相关信息
     */
    @Override
    public DefaultTreeFactory.StrategyAwardVO process(String userId, Long strategyId, Long awardId, Date endDateTime) {
        DefaultTreeFactory.StrategyAwardVO strategyAwardVO = null;

        // 获取基础信息
        String nextNode = ruleTreeVO.getTreeRootRuleNode();
        Map<String, RuleTreeNodeVO> treeNodeMap = ruleTreeVO.getTreeNodeMap();

        // 获取起始节点「根节点记录了第一个要执行的规则」
        RuleTreeNodeVO ruleTreeNode = treeNodeMap.get(nextNode);
        while (null != nextNode) {
            // 获取决策节点
            ILogicTreeNode logicTreeNode = logicTreeNodeGroupMap.get(ruleTreeNode.getRuleKey());
            // 获取规则配置（ruleValue）
            String ruleValue = ruleTreeNode.getRuleValue();
            // 决策节点计算
            DefaultTreeFactory.TreeActionEntity logicEntity = logicTreeNode.logic(userId, strategyId, awardId, ruleValue, endDateTime);
            // 获取结果： allow? or take_over?
            RuleLogicCheckTypeVO ruleLogicCheckTypeVO = logicEntity.getRuleLogicCheckType();
            // 获取奖品信息
            strategyAwardVO = logicEntity.getStrategyAwardVO();
            log.info("决策树引擎【{}】 treeId:{} node:{} code:{}", ruleTreeVO.getTreeName(), ruleTreeVO.getTreeId(), nextNode, ruleLogicCheckTypeVO.getCode());

            // 获取下个节点
            nextNode = nextNode(ruleLogicCheckTypeVO.getCode(), ruleTreeNode.getTreeNodeLineVOList());
            ruleTreeNode = treeNodeMap.get(nextNode);
        }

        // 返回最终结果
        return strategyAwardVO;
    }

    /**
     * 获取下一个节点
     */
    public String nextNode(String matterValue, List<RuleTreeNodeLineVO> treeNodeLineVOList) {
        if (null == treeNodeLineVOList || treeNodeLineVOList.isEmpty()) {
            log.info("规则连线已为空, 不存在下一个规则节点。");
            return null;
        }
        for (RuleTreeNodeLineVO nodeLine : treeNodeLineVOList) {
            if (decisionLogic(matterValue, nodeLine)) {
                return nodeLine.getRuleNodeTo();
            }
        }
        return null;
    }
    /**
     * 控制下一个节点的走向
     */
    public boolean decisionLogic(String matterValue, RuleTreeNodeLineVO nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());
            // 以下规则暂时不需要实现
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }
}
